math number theory *1600

Please click on ads to support us..

Python Code:

global l
global p
def build(a,b):
    global l,p
    l=a*b
    p=[0]*l
    p[0]=0
    for i in range(1,l):
        p[i]=p[i-1]
        if ((i%a)%b)!=((i%b)%a):
            p[i]+=1
def query(x):
    cnt=x//l
    rem=x%l
        return p[-1]*cnt+p[rem]


for _ in range(int(input())):
    a,b,q=map(int,input().split())
    build(a,b)
    for _ in range(q):
        q,r=map(int,input().split())
        print(query(r)-query(q-1),end=" ")
    print()
    

C++ Code:

// https://codeforces.com/problemset/problem/1342/C
#include<bits/stdc++.h>
/*
Gurmeet Singh
MAIT
2024
*/
using namespace std;
#define int long long int
#define mod (int)(1e9+7)
int findcount(int r,int lcm,int b)
{
    int div=r/lcm;
    int prod=div*lcm;
    int total=r;
    if(div!=0)
    {
        total-=min(r-prod+1,b); 
        total-=(div-1)*b;
    }
    return total-min(r,(b-1));
}
void solve()
{
    int a,b,q;
    cin>>a>>b>>q;
    if(a>b) swap(a,b);
    int lcm=(a*b)/gcd(a,b);
    while(q--)
    {
        int l,r;
        cin>>l>>r;
        if(lcm==b || (r<b)) {
            cout<<0<<" ";
            continue;
        }
        int cnt=findcount(r,lcm,b);
        if(l>1) cnt-=findcount(l-1,lcm,b);
        cout<<cnt<<" ";
    }
    cout<<"\n";
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    while(t--)
        solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

361A - Levko and Table
5E - Bindian Signalizing
687A - NP-Hard Problem
1542C - Strange Function
961E - Tufurama
129D - String
888A - Local Extrema
722B - Verse Pattern
278A - Circle Line
940A - Points on the line
1742C - Stripes
1742F - Smaller
1742B - Increasing
1742A - Sum
1742D - Coprime
390A - Inna and Alarm Clock
1398B - Substring Removal Game
1742G - Orray
416B - Art Union
962A - Equator
803B - Distances to Zero
291A - Spyke Talks
1742E - Scuza
1506D - Epic Transformation
1354G - Find a Gift
1426F - Number of Subsequences
1146B - Hate "A"
1718C - Tonya and Burenka-179
834A - The Useless Toy
1407D - Discrete Centrifugal Jumps